import com.sun.jna.*;

import java.util.ArrayList;
import java.util.List;

public class Macitcsi {


    public interface CLibrary extends Library {


        interface FileReceivedCallbackCallback extends Callback {
            int fileReceived(int siteId, String fileName);
        }

        interface GetTmpFileNameCallback extends Callback {
            int getTmpFileName(int deviceId, String filename, int nameLength);
        }

        interface GetCommunicationConfigCallback extends Callback {
            void getCommunicationConfig(int siteId, Pointer pointer);
        }

        void SetFileReceived(FileReceivedCallbackCallback fileReceivedCallbackCallback);

        void SetGetTmpFileName(GetTmpFileNameCallback getTmpFileNameCallback);

        void SetGetCommunicationConfig(GetCommunicationConfigCallback getCommunicationConfigCallback);

        void InitializeCSILibrary();

        boolean CreateCSIInstance(int deviceId, boolean bSquelchLogging);


        public static class CommunicationConfig extends Structure {

            public CommunicationConfig(Pointer p){
                super(p);
            }

            public byte[] ComputerName = new byte[16];

            public byte NetworkType;

            public int Baudrate;

            public byte Function;

            public byte Port;

            public byte Parity;

            public byte StopBits;

            public byte DataBits;

            public byte FlowControl;

            public byte DCProtocol;

            public byte FaxFlowControl;

            public short DSRTimeout;

            public short CTSTimeout;

            public byte[] InitString = new byte[256];

            public int AllowTempDevice;

            public int AllowAllDevice;

            public byte[] NetworkTarget = new byte[65];

            public int NetworkPort;

            public int RetryInterval;

            /**
             * Return this Structure's field names in their proper order.  For
             * example,
             * <pre><code>
             * protected List getFieldOrder() {
             *     return Arrays.asList(new String[] { ... });
             * }
             * </code></pre>
             * <strong>IMPORTANT</strong>
             * When deriving from an existing Structure subclass, ensure that
             * you augment the list provided by the superclass, e.g.
             * <pre><code>
             * protected List getFieldOrder() {
             *     List fields = new ArrayList(super.getFieldOrder());
             *     fields.addAll(Arrays.asList(new String[] { ... }));
             *     return fields;
             * }
             * </code></pre>
             * <p>
             * Field order must be explicitly indicated, since the
             * field order as returned by {@link Class#getFields()} is not
             * guaranteed to be predictable.
             *
             * @return ordered list of field names
             */
            @Override
            protected List getFieldOrder() {
                List a = new ArrayList();
                a.add("ComputerName");
                a.add("NetworkType");
                a.add("Baudrate");
                a.add("Function");
                a.add("Port");

                a.add("Parity");
                a.add("StopBits");
                a.add("DataBits");
                a.add("FlowControl");
                a.add("DCProtocol");

                a.add("FaxFlowControl");
                a.add("DSRTimeout");
                a.add("CTSTimeout");
                a.add("InitString");
                a.add("AllowTempDevice");

                a.add("AllowAllDevice");
                a.add("NetworkTarget");
                a.add("NetworkPort");
                a.add("RetryInterval");

                return a;
            }
        }

    }


    public static void main(String[] args)  {

        CLibrary lib = (CLibrary) Native.loadLibrary("C:\\workspace\\angel-bo\\angel-common\\src\\test\\java\\macitcsi.dll", CLibrary.class);

        CLibrary.FileReceivedCallbackCallback fileReceivedCallbackCallback = new CLibrary.FileReceivedCallbackCallback() {
            @Override
            public int fileReceived(int siteId, String fileName) {
                System.out.println("fileReceived");
                return 0;
            }
        };

        CLibrary.GetTmpFileNameCallback getTmpFileNameCallback = new CLibrary.GetTmpFileNameCallback() {
            @Override
            public int getTmpFileName(int deviceId, String filename, int nameLength) {
                System.out.println("getTmpFileName");
                return 0;
            }
        };

        CLibrary.GetCommunicationConfigCallback getCommunicationConfigCallback = new CLibrary.GetCommunicationConfigCallback() {
            @Override
            public void getCommunicationConfig(int siteId, Pointer pointer) {

                CLibrary.CommunicationConfig cfg = new CLibrary.CommunicationConfig(pointer);
                //CommunicationConfig cfg = new CommunicationConfig(pointer);
                if (siteId < 5) {
                    cfg.NetworkTarget = "127.0.0.1".getBytes();
                    cfg.NetworkType = 4;
                    cfg.NetworkPort = 3000 + siteId;
                    cfg.RetryInterval = siteId;

                } else {
                    cfg.NetworkType = 3;
                    cfg.Port =4;
                    cfg.Baudrate =57600;
                    cfg.Parity= 0;
                    cfg.StopBits = 0;
                    cfg.FlowControl = 0;
                }
                cfg.write();
                System.out.println("getCommunicationConfig"+pointer);

            }
        };

        lib.SetFileReceived(fileReceivedCallbackCallback);
        lib.SetGetTmpFileName(getTmpFileNameCallback);
        lib.SetGetCommunicationConfig(getCommunicationConfigCallback);

        lib.InitializeCSILibrary();
        try{
            lib.CreateCSIInstance(6, false);
            Thread.sleep(100000);
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}





